<template>
  <view>
    <!-- 引用自定义组件 -->
    <!-- 验证自定义搜索组件可用通过属性来修改指定的值来增强通用性 -->
    <!-- <my-search></my-search> -->
    <my-search @click="gotoSearch" :bgcolor="'pink'" :radius="18"></my-search>
    
    <view class="scroll-box-view">
      <!-- 左侧的滚动视图区域 -->
      <scroll-view scroll-y class="left-scroll-view" :style="{height: wh + 'px'}">
        <block v-for="(item,i) in cateList" :key="i">
          <view :class="['left-scroll-view-item', i === active ? 'active' : '']" @click="Changacitve(i)">
            {{item.cat_name}}</view>
        </block>
      </scroll-view>

      <!-- 右侧的滚动视图区域 -->
      <scroll-view scroll-y class="right-scroll-view" :style="{height: wh + 'px'}" scroll-top="scrollTop">
        <view v-for="(item2,i2) in catechildren" :key="i2">
          <!-- 二级分类标题 -->
          <view class="right-scroll-view-lb2">/{{item2.cat_name}}/</view>
          <!-- 动态渲染三级分类的列表数据 -->
          <view class="right-scroll-list">
            <!-- 三级分类 Item 项 -->
            <view class="right-scroll-item" v-for="(item3,i3) in item2.children" :key="i3"
              @click="gotoGoodsList(item3)">
              <image :src="item3.cat_icon.replace('dev','web')"></image>
              <text>{{item3.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
// 导入自己封装的 mixin 模块
  import badgeMix from '@/mixins/tabbar-badge.js'
  export default {
     // 将 badgeMix 混入到当前的页面中进行使用(挂载)
    mixins:[badgeMix],
    data() {
      return {
        // 窗口的可用高度 = 屏幕高度 - navigationBar高度 - tabBar 高度
        wh: 0,
        cateList: [],
        active: 0,
        catechildren: [],
        //默认让右侧页面每次都置顶
        scrollTop: 0
      };
    },
    onLoad() {
      // 获取当前系统的信息
      const kd = uni.getSystemInfoSync()
      //获取到了可用屏幕高度（减五十是因为要减掉搜索框的高度）
      this.wh = kd.windowHeight-50
      this.GetcateList()
    },
    methods: {
      async GetcateList() {
        const {
          data: res
        } = await uni.$http.get("https://api-ugo-web.itheima.net/api/public/v1/categories")
        console.log(res.message)
        if (res.meta.status !== 200) return uni.$showMsg()
        this.cateList = res.message
        // 为二级分类赋值(默认获取索引为0的二级分类)
        this.catechildren = res.message[0].children
      },
      //active值改变事件
      Changacitve(i) {
        this.active = i
        // 通过一级分类列表的索引重新为二级分类列表赋值
        this.catechildren = this.cateList[i].children

        //当每次都调用Changacitve就更改scrollTop的值来实现置顶
        this.scrollTop = this.scrollTop === 0 ? 1 : 0
      },
      gotoGoodsList(item3) {
        uni.navigateTo({
          url: "/subpkg/goods_list/goods_list?cid=" + item3.cat_id
        })
      },
      gotoSearch(){
        uni.navigateTo({
          url:'/subpkg/serach/serach'
        })
      }
    }
  }
</script>

<style lang="scss">
  .scroll-box-view {
    display: flex;

    .left-scroll-view {
      width: 130px;

      .left-scroll-view-item {
        font-size: 12px;
        line-height: 60px;
        align-items: center;
        text-align: center;
        background-color: #f7f7f7;
        margin-bottom: 15px;

        &.active {
          background-color: #ffffff;
          position: relative;
          left: 0px;

          &::before {
            content: '';
            display: block;
            height: 30px;
            width: 3px;
            background-color: #c00000;
            position: absolute;
            top: 15px;
            left: 0;
          }
        }
      }
    }

  }

  .right-scroll-view-lb2 {
    font-size: 12px;
    font-weight: bold;
    font-family: "宋体";
    text-align: center;
    padding: 15px 0;
  }

  .right-scroll-list {
    display: flex;
    flex-wrap: wrap;

    .right-scroll-item {
      width: 33.3%;
      display: flex;
      flex-direction: column; //纵向布局
      justify-content: center; //纵向居中
      align-items: center; //横向居中
      margin-bottom: 10px;

      image {
        width: 60px;
        height: 60px;
        margin-bottom: 5px;
      }

      text {
        font-size: 14px;
        font-family: "楷体";
      }
    }
  }
</style>
